package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DealMcMchtFileTaskThread implements Runnable {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    private TMMcTaskFlow taskFlow;

    private String traceNo;
    private String globalInstId;
    private String globalMcNo;
    private String globalMcType;
    private String globalFlowNo;
    private String globalTxnDate;
    private String globalOprId;
    private String globalTimeStamp;
    private String auditStatus;
    private String tmpAuditStatus;
    private String FILE_DEALING = "01";
    private String FILE_DEAL_SUCC = "02";
    private String FILE_DEAL_FAIL = "03";
    private final static String EXECL_XLS = "xls";
    private final static String EXECL_XLSX = "xlsx";
    private boolean mchntListFlag = false;
    private String mcSponsor = "";
    private String mchntStatusMsg = "";
    private String mchntOrgMsg = "";
    private String mchntExistMsg = "";
    private String mchntJoinMsg = "";

    @Override
    public void run() {

        boolean result = false;
        DbsUtil dbsUtil = null;
        dbsUtil = getDbsUtil();
        try {
            globalFlowNo = taskFlow.getFlowNo();
            globalInstId = taskFlow.getInstId();
            globalMcNo = taskFlow.getMcNo();
            globalTxnDate = taskFlow.getTxnDt();
            globalTimeStamp = taskFlow.getCreateTm();
            globalOprId = taskFlow.getOprId();
            traceNo = ChannelEnum.MOMP.getChannelType() + DateUtil.getCurrentDateTime("yyyyMMddHHmmss");

            String bizParam = taskFlow.getBizTypeParamData();
            JSONObject jsonObject = JSONObject.parseObject(bizParam);
            auditStatus = (String) jsonObject.get("auditStatus");
            tmpAuditStatus = (String) jsonObject.get("tmpAuditStatus");

            /** 活动类型 */
            globalMcType = globalMcNo.substring(2, 4);

            rglog.info("机构号<{}>营销活动<{}>交易流水号<{}>,任务流水号<{}>编辑商户任务开始处理", globalInstId, globalMcNo, traceNo, globalFlowNo);

            rglog.info("----step1----:任务流水号<{}>更新任务流水状态", globalFlowNo);
            //变更任务状态为进行中
            dbsUtil.dbsBeginTransaction();
            updateMcTaskStates(dbsUtil, globalFlowNo, FILE_DEALING);
            dbsUtil.dbsEndTransaction(true);

            //处理任务
            rglog.info("----step2----:任务流水号<{}>读取任务文件并处理相关业务逻辑", globalFlowNo);
            result = dealMcMerTask(dbsUtil, taskFlow);
            rglog.info("机构号<{}>营销活动<{}>交易流水号<{}>,任务流水号<{}>更新营销活动正式表及临时表完成", globalInstId, globalMcNo, traceNo, globalFlowNo);

        } catch (Exception e) {
            rglog.error("机构号<{}>营销活动<{}>交易流水号<{}>,任务流水号<{}>读取文件、筛查商户列表、更新营销活动正式表/临时表审批状态操作异常，error=<{}>", globalInstId, globalMcNo, traceNo, globalFlowNo, e.getMessage());
        } finally {
            try {
                rglog.info("----step3----:任务流水号<{}>更新任务表状态,任务结果<{}>", globalFlowNo, result ? FILE_DEAL_SUCC : FILE_DEAL_FAIL);
                dbsUtil.dbsBeginTransaction();
                updateMcTaskStates(dbsUtil, globalFlowNo, result ? FILE_DEAL_SUCC : FILE_DEAL_FAIL);
                rglog.info("机构号<{}>营销活动<{}>交易流水号<{}>,任务流水号<{}>编辑商户任务结束", globalInstId, globalMcNo, traceNo, globalFlowNo);
                dbsUtil.dbsEndTransaction(true);
            } catch (Exception e) {
                e.printStackTrace();
                rglog.info("机构号<{}>营销活动<{}>交易流水号<{}>,任务流水号<{}>更新任务表状态异常，错误信息<{}>", globalInstId, globalMcNo, traceNo, globalFlowNo, e.getMessage());
            }
        }
    }

    /**
     * 更新营销活动正式表与临时表活动数据审批状态
     * 任务执行失败或者符合活动的商户为空时将活动状态置为拒绝状态
     *
     * @param dbsUtil
     * @param result  任务执行结果 true成功，false失败
     */
    private void updateMcAuditStatus(DbsUtil dbsUtil, boolean result) {
        rglog.info("更新营销活动正式表与临时表审批状态");
        try {
            /* 增加商户列表无数据的处理，将审批状态调整为对应的拒绝状态，并备注原因 */
            if (mchntListFlag || !result) {
                rglog.info("机构号<{}>营销活动<{}>交易流水号<{}>,任务流水号<{}>任务处理失败，审批状态置为拒绝状态", globalInstId, globalMcNo, traceNo, globalFlowNo);
                auditStatus = auditStatus.substring(0, 1).concat(CommonConstant.THREE_COMMON_CONSTANT);
                tmpAuditStatus = tmpAuditStatus.substring(0, 1).concat(CommonConstant.THREE_COMMON_CONSTANT);
            }
            /** 减免类 */
            if (McTypeEnum.REDUCTION_FIXED_AMOUNT.getMcTypeCode().equals(globalMcType) || McTypeEnum.REDUCTION_RANDOM_AMOUNT.getMcTypeCode().equals(globalMcType) ||
                    McTypeEnum.REDUCTION_FIXED_RATE.getMcTypeCode().equals(globalMcType) || McTypeEnum.REDUCTION_RANDOM_RATE.getMcTypeCode().equals(globalMcType)) {
                rglog.info("减免类营销活动任务完成，还原活动及临时表审批状态数据");
                /** 更新营销活动表审核状态 */
                updateMOMcAuthStates(dbsUtil);
                updataTMPlaAuditStatusForMchntTask(dbsUtil, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
                /** 卡券类 */
            } else if (McTypeEnum.COUPON_FREE.getMcTypeCode().equals(globalMcType) || McTypeEnum.COUPON_PAID.getMcTypeCode().equals(globalMcType) || McTypeEnum.COUPON_ALL.getMcTypeCode().equals(globalMcType)) {
                rglog.info("卡券类营销活动任务完成，还原活动及临时表审批状态数据");
                updateCPMcAuthStates(dbsUtil);
                updataTMPlaAuditStatusForMchntTask(dbsUtil, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());
                /** 商户手续费类 */
            } else if (McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode().equals(globalMcType)) {
                rglog.info("商户手续费类营销活动任务完成，还原活动及临时表审批状态数据");
                updateMFDMcAuthStates(dbsUtil);
                updataTMPlaAuditStatusForMchntTask(dbsUtil, TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                /** 收银员返现类 */
            } else if (McTypeEnum.CASHER_CASHBACK.getMcTypeCode().equals(globalMcType)) {
                rglog.info("收银员返现类营销活动任务完成，还原活动及临时表审批状态数据");
                updateCCBMcAuthStates(dbsUtil);
                updataTMPlaAuditStatusForMchntTask(dbsUtil, TableNameEnum.T_M_MC_CCB_INFO.getTableNameCode());
            } else {
                rglog.info("任务流水异常，营销活动类型异常");
                throw new BizException(RespCodeEnum.NOT_SUPPORTED_MC_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_MC_TYPE_ERROR.getRespDesc());
            }
        } catch (Exception e) {
            rglog.error("机构号<{}>营销活动<{}>交易流水号<{}>,任务流水号<{}>更新营销活动正式表、临时表审批状态异常，error=<{}>", globalInstId, globalMcNo, traceNo, globalFlowNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    private void updateCCBMcAuthStates(DbsUtil dbsUtil) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        int returnCode = mcDetailInfoMapper.updateCashierCashbackMcAuditStatusByPrimaryKeyForTask(globalInstId, globalMcNo, auditStatus);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("更新收银员返现类营销活动审批状态信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    private void updateCPMcAuthStates(DbsUtil dbsUtil) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        int returnCode = mcDetailInfoMapper.updateCouponMcAuditStatusByPrimaryKeyForTask(globalInstId, globalMcNo, auditStatus);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("更新卡券类营销活动审批状态信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    private void updateMFDMcAuthStates(DbsUtil dbsUtil) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        int returnCode = mcDetailInfoMapper.updateMfdMcAuditStatusByPrimaryKeyForTask(globalInstId, globalMcNo, auditStatus);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("更新手续费减免营销活动审批状态信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }


    private List<String> readExeclFileForMer(String filePath) throws Exception {
        rglog.info("开始读取文件，路径：" + filePath);

        FileInputStream inputStream = null;
        List<String> mchntNoList = new ArrayList<String>();
        //校验文件格式
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在：" + filePath);
        }
        if (!(filePath.endsWith(EXECL_XLS) || filePath.endsWith(EXECL_XLSX))) {
            throw new FileFormatException("文件格式错误：" + filePath);
        }
        Workbook wookbook = null;
        inputStream = new FileInputStream(filePath);
        rglog.info("读取文件:begin");
        if (filePath.endsWith(EXECL_XLS)) {
            wookbook = new HSSFWorkbook(inputStream);
        } else {
            wookbook = new XSSFWorkbook(inputStream);
        }
        rglog.info("读取文件:end");
        //获取第一个页签
        Sheet sheet = wookbook.getSheetAt(0);
        //读取商户信息
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (row != null) {
                Cell obj = row.getCell(0);
                String merNo = obj.getStringCellValue();
                if (!StringUtil.isNullorEmpty(merNo)) {
                    mchntNoList.add(merNo);
                }
            }

        }
        rglog.info("营销活动<{}>任务流水号<{}>商户任务读取文件<{}>成功，商户数量<{}>.", globalMcNo, globalFlowNo, filePath, mchntNoList.size());
        mchntNoList = mchntNoList.stream().distinct().collect(Collectors.toList());
        rglog.info("营销活动<{}>任务流水号<{}>商户文件内容去重操作，商户数量<{}>.", globalMcNo, globalFlowNo, mchntNoList.size());
        return mchntNoList;
    }

    /**
     * 更新任务流水表处理状态信息
     *
     * @param dbsUtil 数据库操作工具类
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/4/1 18:05
     */
    private void updateMcTaskStates(DbsUtil dbsUtil, String flowNo, String fileDealStt) throws Exception {

        McTaskFlowMapper taskFlowMapper = new McTaskFlowMapperImpl();
        int returnCode = taskFlowMapper.updateMcTaskFlowStt(globalInstId, globalMcNo, flowNo, fileDealStt);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("更新营销活动商户任务流水状态异常, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>, TASK_STS=<{}>", returnCode, globalInstId, globalMcNo, fileDealStt);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    /**
     * 读取数据并执行任务
     *
     * @param taskFlow
     */
    private boolean dealMcMerTask(DbsUtil dbsUtil, TMMcTaskFlow taskFlow) {
        boolean result = false;
        try {
            String filePath = taskFlow.getFilePath();
            /** 读取商户文件 */
            List<String> mchntNoList = readExeclFileForMer(filePath);
            /** 查询对应的商户信息 */
            List<TBMchntBaseInfo> mcMchntInfoList = selectMcMchntInfoList(mchntNoList);
            /** 查询营销活动机构列表 */
            List<TMMcOrgInfo> orgInfoList = queryMcOrgInfoList();
            /** 筛选商户列表，保留符合条件的商户数据 */
            checkMchntAndOrgInfo(orgInfoList, mcMchntInfoList);
            rglog.info("营销活动<{}>任务流水号<{}>商户列表筛查结果MSG:无效商户<{}>，机构不匹配商户<{}>，特殊费率商户<{}>，已参加手续费营销活动商户<{}>。", globalMcNo, globalFlowNo, mchntStatusMsg, mchntOrgMsg, mchntExistMsg, mchntJoinMsg);
            if (mcMchntInfoList.isEmpty()) {
                mchntListFlag = true;
                return false;
            }

            //对客活动查询活动信息
            McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();

            if (McTypeEnum.REDUCTION_FIXED_AMOUNT.getMcTypeCode().equals(globalMcType) || McTypeEnum.REDUCTION_RANDOM_AMOUNT.getMcTypeCode().equals(globalMcType) ||
                    McTypeEnum.REDUCTION_FIXED_RATE.getMcTypeCode().equals(globalMcType) || McTypeEnum.REDUCTION_RANDOM_RATE.getMcTypeCode().equals(globalMcType)) {
                TMMcMoneyOffInfo tmMcMoneyOffInfo = (TMMcMoneyOffInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, globalMcNo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
                mcSponsor = tmMcMoneyOffInfo.getSponsor();

                /** 卡券类 */
            } else if (McTypeEnum.COUPON_FREE.getMcTypeCode().equals(globalMcType) || McTypeEnum.COUPON_PAID.getMcTypeCode().equals(globalMcType) || McTypeEnum.COUPON_ALL.getMcTypeCode().equals(globalMcType)) {
                TMMcCouponInfo tmMcMoneyOffInfo = (TMMcCouponInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, globalMcNo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());
                mcSponsor = tmMcMoneyOffInfo.getSponsor();
            }

            List<TMMcMchntInfo> mcMchntList = mcMchntInfoList.stream().map(this::convertMcMchntInfo).collect(Collectors.toList());

            //清除原商户列表数据
            //判断营销活动是否已经存在商户信息，只查询商户信息
            McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
            TMMcMchntInfo tmMcMchntInfo = mcMchntInfoMapper.selectMcMchntInfoListCount(globalInstId, globalMcNo, CommonConstant.ZERO_COMMON_CONSTANT);
            int mcMchntListSize = Integer.valueOf(tmMcMchntInfo.getRemark5());
            rglog.info("营销活动：<{}>查询营销活动是否存在存量商户列表,如存在则清除原活动商户信息", globalMcNo);
            rglog.info("开启事务");
            //开启数据库事务
            dbsUtil.dbsBeginTransaction();
            if (mcMchntListSize > 0) {
                rglog.info("营销活动：<{}>删除存量商户列表,size=<{}>", globalMcNo, mcMchntListSize);
                deleteMcMchntInfoByType(dbsUtil, CommonConstant.ZERO_COMMON_CONSTANT);
                deletePlatBizTmpByMchntType(dbsUtil, CommonConstant.ZERO_COMMON_CONSTANT);
            }
            /** 保存营销活动商户信息 */
            rglog.info("营销活动：<{}>营销活动商户表保存商户数据，size=<{}>", globalMcNo, mcMchntList.size());
            insertMcMchntInfo(dbsUtil, mcMchntList);
            /** 保存业务表数据 */
            List<TMPlatBizTmp> tmpMchntList = mcMchntList.stream().map(this::convertMcMchntInfoToTmp).collect(Collectors.toList());
            rglog.info("营销活动：<{}>临时表保存营销活动商户信息，size=<{}>", globalMcNo, tmpMchntList.size());
            insertMultiMcDetialInfoByBank(dbsUtil, tmpMchntList);
            //提交事务
            dbsUtil.dbsEndTransaction(true);
            rglog.info("step2:商户数据处理完成，提交事务");
            result = true;
        } catch (Exception e) {
            rglog.info("step2:营销活动任务流水处理异常,回滚营销活动相关数据操作，errorMsg:" + e.getMessage());
            dbsUtil.dbsEndTransaction(false);
        } finally {
            rglog.info("营销活动：<{}>,任务流水号<{}>更新活动正式表及临时表状态", globalMcNo, globalFlowNo);
            //开启数据库事务
            dbsUtil.dbsBeginTransaction();
            updateMcAuditStatus(dbsUtil, result);
            dbsUtil.dbsEndTransaction(true);
            rglog.info("营销活动：<{}>,任务流水号<{}>更新活动正式表及临时表状态,提交事务", globalMcNo, globalFlowNo);
        }
        return result;
    }

    /**
     * 获取数据库操作实例
     *
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/4/2 10:05
     */
    private DbsUtil getDbsUtil() {
        /* 获取数据库实例 */
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        return dbsUtil;
    }


    /**
     * 更新营销活动审批状态信息
     *
     * @param dbsUtil 数据库操作工具类
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/4/1 18:05
     */
    private void updateMOMcAuthStates(DbsUtil dbsUtil) throws Exception {
        if (!StringUtil.isNullorEmpty(auditStatus)) {
            //更新活动表审核状态
            McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
            int returnCode = mcDetailInfoMapper.updateMoneyOffMcAuditStatusByPrimaryKeyForTask(globalInstId, globalMcNo, auditStatus);
            if (Database.DBS_SUCCESS != returnCode) {
                /* 异常结束数据库事务 */
                rglog.error("更新减免类营销活动审批状态失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, globalInstId, globalMcNo);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        }
    }

    private void updataTMPlaAuditStatusForMchntTask(DbsUtil dbsUtil, String tableName) throws Exception {
        if (!StringUtil.isNullorEmpty(tmpAuditStatus)) {
            //更新活动表审核状态
            PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
            int returnCode = platBizTmpMapper.updataTMPlaAuditStatusForMchntTask(globalInstId, globalMcNo, tmpAuditStatus, tableName);
            if (Database.DBS_SUCCESS != returnCode) {
                /* 异常结束数据库事务 */
                rglog.error("更新营销活动<{}>临时表活动审批状态<{}>信息失败, RETURN_CODE=<{}>, INST_ID=<{}>", globalMcNo, tmpAuditStatus, returnCode, globalInstId);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        }
    }


    /**
     * 查询营销活动机构信息表
     *
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/3/29 16:38
     */
    private List<TMMcOrgInfo> queryMcOrgInfoList() throws Exception {
        McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
        return mcOrgInfoMapper.selectMcOrgInfoList(globalInstId, globalMcNo);
    }

    /**
     * 删除营销活动商户信息
     *
     * @param dbsUtil   数据库操作工具类
     * @param mchntType 商户范围类型
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/4/1 18:05
     */
    private void deleteMcMchntInfoByType(DbsUtil dbsUtil, String mchntType) throws Exception {

        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        int returnCode = mcMchntInfoMapper.deleteMchntInfoByType(globalInstId, globalMcNo, mchntType);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("删除营销活动商户信息失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, globalInstId, globalMcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    /**
     * 删除临时表商户数据
     *
     * @param dbsUtil
     * @param mchntType 商户范围类型
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/8/12 18:05
     */
    private void deletePlatBizTmpByMchntType(DbsUtil dbsUtil, String mchntType) throws Exception {
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        int returnCode = platBizTmpMapper.deletePlatBizTmpByMchntType(globalInstId, globalMcNo, mchntType);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("删除营销活动商户信息失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, globalInstId, globalMcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    /**
     * 插入营销活动商户信息
     *
     * @param mcMchntInfoList 营销活动商户信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/4/1 17:05
     */
    private void insertMcMchntInfo(DbsUtil dbsUtil, List<TMMcMchntInfo> mcMchntInfoList) throws Exception {
        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        int returnCode = mcMchntInfoMapper.insertMcMchntInfoListByBank(mcMchntInfoList);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("新增营销活动商户信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    /**
     * 将营销活动机构信息插入临时表中
     *
     * @param dbsUtil          数据库操作工具类
     * @param tmPlatBizTmpList 业务数据临时表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020/5/28 22:17
     */
    private void insertMultiMcDetialInfoByBank(DbsUtil dbsUtil, List<TMPlatBizTmp> tmPlatBizTmpList) throws Exception {
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        int returnCode = platBizTmpMapper.insertMultiMcDetialInfoByBank(tmPlatBizTmpList);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("新增营销活动商户信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    private void checkMchntAndOrgInfo(List<TMMcOrgInfo> mcOrgInfoList, List<TBMchntBaseInfo> mchntList) throws Exception {

        rglog.info("营销活动：<{}>校验商户所属机构与活动机构是否匹配，检查开始，商户列表size=<{}>", globalMcNo, mchntList.size());

        //处理查询的机构信息
        List<String> orgIdList = mcOrgInfoList.stream().map(item -> item.getOrgId()).collect(Collectors.toList());
        //对机构进行去重处理
        orgIdList = orgIdList.stream().distinct().collect(Collectors.toList());
        int maxLength = mchntList.size();
        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
        //获取当前时间
        String date = DateUtil.getCurrentDate();
        for (int i = 0; i < maxLength; i++) {
            TBMchntBaseInfo mcMchntInfo = mchntList.get(i);
            if (!orgIdList.contains(mcMchntInfo.getSignInstId())) {
                rglog.info("商户号：<{}>, 机构信息与商户所属机构信息校验不通过，去除该商户！", mcMchntInfo.getMchntNo());
                mchntOrgMsg += mcMchntInfo.getMchntNo() + ",";
                mchntList.remove(mcMchntInfo);
                maxLength--;
                i--;
                continue;
            }
            //如果是手续费活动，需要进行其他校验
            if (McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode().equals(globalMcType)) {
                String mchntNo = mcMchntInfo.getMchntNo();
                //判断商户是否存在特殊费率，如果有则不允许进行下一步
                List<TBMchntFeeInfo> tbMchntFeeInfoList = mchntInfoMapper.selectMchntFeeInfoListByMchntNo(globalInstId, mchntNo);
                if (null != tbMchntFeeInfoList && tbMchntFeeInfoList.size() > 0) {
                    tbMchntFeeInfoList = tbMchntFeeInfoList.stream().filter(item -> item.getFeeInputMode().equals(CommonConstant.ZERO_COMMON_CONSTANT)).collect(Collectors.toList());
                    if (tbMchntFeeInfoList.size() > 0) {
                        rglog.error("交易流水号：<{}>, 商户<{}>费率信息表存在特殊费率！", traceNo, mchntNo);
                        mchntExistMsg += mchntNo + ",";
                        mchntList.remove(mcMchntInfo);
                        maxLength--;
                        i--;
                        continue;
                    }
                }
                //查询手续费达标统计表是否有该商户现交易日期达标的数据
                TMStatMfdCriteriaMapper tmStatMfdCriteriaMapper = new TMStatMfdCriteriaMapperImpl();
                TMStatMfdCriteria tmStatMfdCriteria = tmStatMfdCriteriaMapper.selectTMStatMfdCriteriaByUsingMchntNoAndDateRange(globalInstId, mchntNo, date);
                if (null != tmStatMfdCriteria) {
                    /* 营销活动信息正式表查询 */
                    McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
                    TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, tmStatMfdCriteria.getMcNo(), TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                    //如果达标统计表有当前交易日期商户达标的数据，则直接拒绝新增,原活动状态为自然结束，强制结束，已结束的数据作废
                    if (null != tmMcMchntFeeDiscountInfo && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())) {
                        rglog.error("交易流水号：<{}>, 商户<{}>已参加其他手续费活动<{}>！", traceNo, mchntNo, tmMcMchntFeeDiscountInfo.getMcNo());
                        mchntJoinMsg += mchntNo + "_" + tmMcMchntFeeDiscountInfo.getMcNo() + ",";
                        mchntList.remove(mcMchntInfo);
                        maxLength--;
                        i--;
                        continue;
                    }
                }
            }
        }

        if (McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode().equals(globalMcType)) {
            //根据活动编号查询活动主表开始日期
            McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
            TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, globalMcNo, TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
            if (tmMcMchntFeeDiscountInfo != null && CommonConstant.STOCK_MCHNT_FLAG_ONE.equals(tmMcMchntFeeDiscountInfo.getStockMchntFlag())) {
                //判断未上送商户号商圈号时，机构下的非特殊费率商户达标统计表是否存该交易日期内的数据
                String mcStartDate = tmMcMchntFeeDiscountInfo.getMcStartDate();
                checkMchntNoAndAreaNo(mcStartDate, mchntList);
            }


        }
        rglog.info("营销活动：<{}>校验商户所属机构与活动机构是否匹配，检查完成,商户列表size=<{}>", globalMcNo, mchntList.size());
    }

    /**
     * 判断未上送商户号商圈号时，机构下的非特殊费率商户达标统计表是否存该交易日期内的数据
     *
     * @param mcStartDate         活动开始日期
     * @param mcMchntInfoBeanList 商户列表
     * @throws Exception
     */
    private void checkMchntNoAndAreaNo(String mcStartDate, List<TBMchntBaseInfo> mcMchntInfoBeanList) throws Exception {
        //法人机构号
        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        //查询当前日期内营销活动
        List<TMMcMchntFeeDiscountInfo> tmMcMchntFeeDiscountInfoList = mcInfoListMapper.selectMchntFeeDiscountMcListInfoForTime(globalInstId, mcStartDate, "", "");
        //如果活动不存在，直接返回
        if (null == tmMcMchntFeeDiscountInfoList || tmMcMchntFeeDiscountInfoList.size() == 0) {
            return;
        }
        //循环取商户信息表存在该商户的活动
        int maxLength = mcMchntInfoBeanList.size();
        for (int i = 0; i < maxLength; i++) {
            TBMchntBaseInfo mcMchntInfoBean = mcMchntInfoBeanList.get(i);
            String mchntNo = mcMchntInfoBean.getMchntNo();
            //查询营销商户表数据
            McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
            List<TMMcMchntInfo> tmMcMchntInfoList = mcMchntInfoMapper.selectMfdMchntInfoListByinstIdMchntNo(globalInstId, mchntNo);
            if (null == tmMcMchntInfoList || tmMcMchntInfoList.size() == 0) {
                continue;
            }
            rglog.info("查询营销活动商户信息表数据该商户<{}>数据,共<{}>条", mchntNo, tmMcMchntInfoList.size());
            tmMcMchntInfoList = tmMcMchntInfoList.stream().filter(item -> item.getMcNo().contains("MC20")).collect(Collectors.toList());
            rglog.info("属于银行对商户手续费营销活动的条数<{}>", tmMcMchntInfoList.size());
            if (tmMcMchntInfoList.size() == 0) {
                continue;
            }
            tmMcMchntInfoList = tmMcMchntInfoList.stream().filter(item -> !item.getNewMchntFlag().equals(CommonConstant.MCHNT_REFUSE_FLAG) && !item.getExitFlag().equals(CommonConstant.MC_EXIT_FLAG)).collect(Collectors.toList());
            rglog.info("符合银行对商户手续费营销活动的条数<{}>", tmMcMchntInfoList.size());
            if (tmMcMchntInfoList.size() == 0) {
                continue;
            }
            //上送商户是否参加了当前日期内其他进行中的手续费营销活动(new_mchnt_flag为1 exit_flag为1) (new_mchnt_flag为0 exit_flag为0)(new_mchnt_flag为0 exit_flag为1)
            List<TMMcMchntInfo> jxzMchntList = tmMcMchntInfoList.stream().filter(item -> (item.getNewMchntFlag().equals(CommonConstant.MCHNT_OLD_FLAG) && item.getExitFlag().equals(CommonConstant.MC_NOT_EXIT_FLAG)) || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_NEW_FLAG) && item.getExitFlag().equals(CommonConstant.MC_EXIT_FLAG))
                    || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_OLD_FLAG) && item.getExitFlag().equals(CommonConstant.MC_EXIT_FLAG))).collect(Collectors.toList());
            rglog.info("营销活动商户信息表状态为(new_mchnt_flag为1 exit_flag为1)或(new_mchnt_flag为0 exit_flag为0)或(new_mchnt_flag为0 exit_flag为1)的条数<{}>", jxzMchntList.size());
            if (jxzMchntList.size() > 0) {
                for (TMMcMchntInfo tmMcMchntInfo : jxzMchntList) {
                    /* 营销活动信息正式表查询 */
                    McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
                    TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, tmMcMchntInfo.getMcNo(), TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                    //如果活动状态不是自然结束，强制结束，草稿；审批状态不是新增拒绝，审批拒绝，发布拒绝的状态，直接抛异常
                    if (null != tmMcMchntFeeDiscountInfo && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())
                            && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode()) && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode()) && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.PUBLISH_AUDIT_REJECT.getAuditStatusCode())
                            && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.DRAFT.getMcStatusCode())) {
                        rglog.error("商户<{}>已参加其他进行中手续费活动<{}>,移除该商户！", tmMcMchntInfo.getMchntNo(), tmMcMchntFeeDiscountInfo.getMcNo());
                        mcMchntInfoBeanList.remove(mcMchntInfoBean);
                        maxLength--;
                        i--;
                        continue;
                    }
                }
            }
            //上送商户是否有从a活动到b活动的转移(new_mchnt_flag为2 exit_flag为2) (new_mchnt_flag为1 exit_flag为2)
            List<TMMcMchntInfo> zyMchntList = tmMcMchntInfoList.stream().filter(item -> (item.getNewMchntFlag().equals(CommonConstant.MCHNT_REFUSE_FLAG) && item.getExitFlag().equals(CommonConstant.MC_TRANSFER_FLAG)) || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_NEW_FLAG) && item.getExitFlag().equals(CommonConstant.MC_TRANSFER_FLAG))).collect(Collectors.toList());
            rglog.info("营销活动商户信息表状态为(new_mchnt_flag为2 exit_flag为2)或(new_mchnt_flag为1 exit_flag为2)的条数<{}>", zyMchntList.size());
            if (zyMchntList.size() > 0) {
                for (TMMcMchntInfo tmMcMchntInfo : zyMchntList) {
                    /* 营销活动信息正式表查询 */
                    McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
                    TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, tmMcMchntInfo.getMcNo(), TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                    if (null != tmMcMchntFeeDiscountInfo && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())) {
                        rglog.error("商户<{}>正在进行活动转移,移除该商户！", tmMcMchntInfo.getMchntNo(), tmMcMchntFeeDiscountInfo.getMcNo());
                        mcMchntInfoBeanList.remove(mcMchntInfoBean);
                        maxLength--;
                        i--;
                        continue;
                    }
                }
            }
        }

    }

    private List<TBMchntBaseInfo> selectMcMchntInfoList(List<String> mchntNoList) throws Exception {
        List<TBMchntBaseInfo> mchntInfoList = new ArrayList<>();
        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
        mchntNoList = mchntNoList.stream().distinct().collect(Collectors.toList());
        for (String mchntNo : mchntNoList) {
            //查询商户基本信息
            TBMchntBaseInfo tbMchntBaseInfo = mchntInfoMapper.selectMchntBaseInfoByMchntNo(globalInstId, mchntNo);
            if (null == tbMchntBaseInfo) {
                rglog.error("交易流水号：<{}>, 商户<{}>信息查询异常，！", traceNo, mchntNo);
                mchntStatusMsg += mchntNo + ",";
            } else {
                mchntInfoList.add(tbMchntBaseInfo);
            }
        }
        rglog.info("营销活动<{}>任务流水号<{}>商户信息查询筛选，状态正常的商户数量<{}>.", globalMcNo, globalFlowNo, mchntInfoList.size());
        return mchntInfoList;
    }

    /**
     * 营销活动商户信息数据库实体类和接口实体类数据转换
     *
     * @param mchntBaseInfo 商户信息对象
     * @return cc.rengu.igas.momp.common.entity.TMMcMchntInfo 营销活动商户信息表
     * @author liujinan
     * @since 2021/4/1 14:10
     */
    private TMMcMchntInfo convertMcMchntInfo(TBMchntBaseInfo mchntBaseInfo) {
        TMMcMchntInfo mcMchntInfo = new TMMcMchntInfo();
        try {
            /* 法人机构号 */
            mcMchntInfo.setInstId(globalInstId);
            /* 营销活动编号 */
            mcMchntInfo.setMcNo(globalMcNo);

            //商户信息
            /* 商户范围类型0-商户 */
            mcMchntInfo.setMchntType(CommonConstant.ZERO_COMMON_CONSTANT);
            /* 商户范围号 */
            mcMchntInfo.setMchntNo(mchntBaseInfo.getMchntNo());
            /* 商户范围名称 */
            mcMchntInfo.setMchntName(mchntBaseInfo.getMchntName());

            //非对客营销活动
            if (StringUtil.isNullorEmpty(mcSponsor)) {
                /* 新增商户标志 */
                mcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_OLD_FLAG);
                /* 退出标志 */
                mcMchntInfo.setExitFlag(CommonConstant.MC_NOT_EXIT_FLAG);

            } else {
                if (CommonConstant.SPONSOR_MIXED.equals(mcSponsor)) {
                    /* 联合营销活动初始数据 */

                    /* 新增商户标志 */
                    mcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_OLD_FLAG);
                    /* 退出标志 */
                    mcMchntInfo.setExitFlag(CommonConstant.MC_EXIT_FLAG);
                } else {
                    /* 银行全资或商户全资初始数据 */

                    /* 新增商户标志 */
                    mcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_OLD_FLAG);
                    /* 退出标志 */
                    mcMchntInfo.setExitFlag(CommonConstant.MC_NOT_EXIT_FLAG);
                }
            }

            /* 达标日期 */
            mcMchntInfo.setQualifiedDate(globalTxnDate);
            /* 创建人 */
            mcMchntInfo.setOprId(globalOprId); //取活动创建人
            /* 创建时间 */
            mcMchntInfo.setCreateTime(globalTimeStamp);//取活动创建时间

        } catch (Exception e) {
            rglog.error("转换营销活动商户信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return mcMchntInfo;
    }

    /**
     * 营销活动商户信息表数据转换
     * 正式表数据转换为临时表数据
     *
     * @param tmMcMchntInfo 营销活动商户信息表
     * @return cc.rengu.igas.momp.common.entity.TMPlatBizTmp 业务数据临时表
     * @author Jinan Liu
     * @since 2020/5/28 22:26
     */
    private TMPlatBizTmp convertMcMchntInfoToTmp(TMMcMchntInfo tmMcMchntInfo) {

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();

        try {
            /* 法人机构标识 */
            tmPlatBizTmp.setInstId(tmMcMchntInfo.getInstId());
            /* 营销活动编号 */
            tmPlatBizTmp.setMcNo(tmMcMchntInfo.getMcNo());
            /* 商户编号 */
            tmPlatBizTmp.setMchntNo(tmMcMchntInfo.getMchntNo());
            /* 操作菜单 */
            tmPlatBizTmp.setOprMenuId(CommonConstant.DEFAULT_MENU_ID);
            /* 操作菜单名 */
            tmPlatBizTmp.setOprMenuName(CommonConstant.DEFAULT_MENU_NAME);
            /* 操作表中文名 */
            tmPlatBizTmp.setOprTableCname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameDesc());
            /* 操作表英文名 */
            tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode());
            /* 操作 */
            tmPlatBizTmp.setBizOpr(BizOprEnum.INSERT.getBizOprCode());
            /* 新增商户标志 */
            tmMcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_OLD_FLAG);
            /* 退出标志 */
            tmMcMchntInfo.setExitFlag(CommonConstant.MC_NOT_EXIT_FLAG);
            /* 达标日期 */
            tmMcMchntInfo.setQualifiedDate(globalTxnDate);
            /* 数据域1 - 拼接JSON字符串 */
            tmPlatBizTmp.setBizData1(JsonOperation.packMcMchntInfoTmpBizData1(tmMcMchntInfo, rglog));
            /* 状态 */
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            /* 营销活动状态 */
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            /* 审批状态 */
            tmPlatBizTmp.setAuditStatus(AuditStatusEnum.WAIT_FOR_SUBMIT.getAuditStatusCode());
            /* 创建人 */
            tmPlatBizTmp.setOprId(globalOprId);
            /* 创建时间 */
            tmPlatBizTmp.setCreateTime(globalTimeStamp);
            /* 使用remark1字段存储商户范围 */
            tmPlatBizTmp.setRemark1(tmMcMchntInfo.getMchntType());

            return tmPlatBizTmp;

        } catch (Exception e) {
            rglog.error("公共业务数据不可为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
    }


    /**
     * 营销活动商户信息表数据转换
     * 正式表数据转换为临时表数据(进行中新增商户)
     *
     * @param tmMcMchntInfo 营销活动商户信息表
     * @return cc.rengu.igas.momp.common.entity.TMPlatBizTmp 业务数据临时表
     * @author JL Pang
     * @since 2020/5/28 22:26
     */
    private TMPlatBizTmp convertMcMchntInfoToTmps(TMMcMchntInfo tmMcMchntInfo) {

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();

        try {
            /* 法人机构标识 */
            tmPlatBizTmp.setInstId(tmMcMchntInfo.getInstId());
            /* 营销活动编号 */
            tmPlatBizTmp.setMcNo(tmMcMchntInfo.getMcNo());
            //商户名称
            tmPlatBizTmp.setMchntName(tmMcMchntInfo.getMchntName());
            /* 商户编号 */
            tmPlatBizTmp.setMchntNo(tmMcMchntInfo.getMchntNo());
            /* 操作菜单 */
            tmPlatBizTmp.setOprMenuId(CommonConstant.DEFAULT_MENU_ID);
            /* 操作菜单名 */
            tmPlatBizTmp.setOprMenuName(CommonConstant.DEFAULT_MENU_NAME);
            /* 操作表中文名 */
            tmPlatBizTmp.setOprTableCname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameDesc());
            /* 操作表英文名 */
            tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode());
            /* 操作 */
            tmPlatBizTmp.setBizOpr(BizOprEnum.INSERT.getBizOprCode());
            /* 新增商户标志 */
            tmMcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_OLD_FLAG);
            /* 退出标志 */
            tmMcMchntInfo.setExitFlag(CommonConstant.MC_NOT_EXIT_FLAG);
            /* 达标日期 */
            tmMcMchntInfo.setQualifiedDate(globalTxnDate);
            /* 数据域1 - 拼接JSON字符串 */
            tmPlatBizTmp.setBizData1(JsonOperation.packMcMchntInfoTmpBizData1(tmMcMchntInfo, rglog));
            /* 状态 */
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            /* 营销活动状态 */
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            /* 审批状态 */
            tmPlatBizTmp.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
            /* 创建人 */
            tmPlatBizTmp.setOprId(globalOprId);
            /* 创建时间 */
            tmPlatBizTmp.setCreateTime(globalTimeStamp);

            return tmPlatBizTmp;

        } catch (Exception e) {
            rglog.error("公共业务数据不可为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
    }

    public void setTaskFlow(TMMcTaskFlow taskFlow) {
        this.taskFlow = taskFlow;
    }
}
